#!/usr/bin/env bash
# Copyright (c) 2021 The Toltec Contributors
# SPDX-License-Identifier: MIT

pkgnames=(draft)
pkgdesc="Launcher which wraps around the standard interface"
url=https://github.com/dixonary/draft-reMarkable
pkgver=0.2.0-25
timestamp=2020-07-20T10:23Z
section="launchers"
maintainer="Mattéo Delabre <spam@delab.re>"
license=Apache-2.0
installdepends=(xochitl display procps-ng-ps)
flags=(patch_rm2fb)

image=qt:v2.1
source=(
    https://github.com/dixonary/draft-reMarkable/archive/5bd660a2fd07eba166c6110d2b48cfc58ee67e58.zip
    draft.service
    launcherctl-draft
)
sha256sums=(
    c41d7a4fd537c54d787018fd764421dbf7dd64306ca800875283e05eef99173e
    SKIP
    SKIP
)

build() {
    qmake draft.pro
    make
}

package() {
    install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/draft

    install -d "$pkgdir"/opt/share/draft "$pkgdir"/usr/share
    ln -s /opt/share/draft "$pkgdir"/usr/share/draft
    cp -r "$srcdir"/qml "$pkgdir"/opt/share/draft
    cp -r "$srcdir"/js "$pkgdir"/opt/share/draft

    install -d "$pkgdir"/opt/etc/draft "$pkgdir"/etc
    ln -s /opt/etc/draft "$pkgdir"/etc/draft
    cp -r "$srcdir"/extra-files/draft/* "$pkgdir"/opt/etc/draft
    rm "$pkgdir"/opt/etc/draft/01-xochitl
    rm "$pkgdir"/opt/etc/draft/icons/xochitl.png
    rm "$pkgdir"/opt/etc/draft/02-fingerterm
    rm "$pkgdir"/opt/etc/draft/icons/fingerterm.png
    mv "$pkgdir"/opt/etc/draft/{99-,}shutdown

    install -D -m 644 -t "$pkgdir"/lib/systemd/system "$srcdir"/draft.service
    install -D -T -m 755 "$srcdir"/launcherctl-draft "$pkgdir"/opt/share/launcherctl/"$pkgname"
}

configure() {
    systemctl daemon-reload

    if ! launcherctl is-current-launcher "$pkgname"; then
        echo ""
        echo "Run the following command(s) to use $pkgname as your launcher"
        echo "launcherctl switch-launcher --start $pkgname"
        echo ""
    fi
}

preremove() {
    # Just in case more than one launcher is active, do individual checks
    if launcherctl is-active-launcher "$pkgname" || launcherctl is-enabled-launcher "$pkgname"; then
        launcherctl switch-launcher --start xochitl
    fi
}

postremove() {
    systemctl daemon-reload
}
